package com.baibei.ebec.follow.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.module.AppRouter;
import com.bumptech.glide.Glide;

import org.json.JSONArray;


/**
 * Created by tang on 2018/8/15.
 */

public class MyFollowAdapter extends RecyclerView.Adapter<MyFollowAdapter.ViewHolder> implements View.OnClickListener {

    private JSONArray datas;
    private String btnDesc;
    private Context context;

    public MyFollowAdapter(JSONArray datas, String btnDesc, Context context) {
        this.datas = datas;
        this.btnDesc = btnDesc;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_follow, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int i) {
        viewHolder.tvName.setText(datas.optJSONObject(i).optString("userName"));
        viewHolder.tvName.setTag(i);
        viewHolder.tvName.setOnClickListener(this);
        viewHolder.tvBtn.setText(btnDesc);
        viewHolder.tvBtn.setTag(i);
        viewHolder.tvBtn.setOnClickListener(this);
        Glide.with(context).load(datas.optJSONObject(i).optString("userPic")).into(viewHolder.iv);
        viewHolder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppRouter.routeToFollowerDetail(context,datas.optJSONObject(i).optString("followerId"));
            }
        });
    }

    public enum  ViewName{
        IMAGE,
        BTN
    }

    @Override
    public int getItemCount() {
        return datas.length();
    }

    MyItemClickListener listener;

    @Override
    public void onClick(View view) {
        int tag = (int) view.getTag();
        if (listener != null){
            if (view.getId() == R.id.tv_btn){
                listener.onClick(view,ViewName.BTN,tag);
            } else {
                listener.onClick(view,ViewName.IMAGE,tag);
            }
        }
    }

    public interface MyItemClickListener{
        void onClick(View view,ViewName viewName,int position);
    }

    public void setOnItemClick(MyItemClickListener listener){
        this.listener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvName;
        private final TextView tvBtn;
        private final ImageView iv;

        public ViewHolder(View view) {
            super(view);
            tvName = view.findViewById(R.id.tv_name);
//            view.findViewById(R.id.tv_btn)
            tvBtn = view.findViewById(R.id.tv_btn);
            iv = view.findViewById(R.id.iv);
        }
    }
}
